package com.example.www.Service;

import com.example.www.Entity.Video;
import com.example.www.Entity.VideoCover;
import com.example.www.Entity.VideoLink;
import com.example.www.Mapper.VideoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Value("${qiniu.url}")
    private String qiniuUrl;

    public List<VideoCover> getVideoCoversList(){
        QueryWrapper<Video> wrapper=new QueryWrapper<>();
        wrapper.select("id","image");
        List<Video> videos=videoMapper.selectList(wrapper);
        List<VideoCover> videoCovers=new ArrayList<>();
        for (Video video: videos) {
            VideoCover videoCover=new VideoCover();
            videoCover.setId(video.getId());
            videoCover.setTitle(video.getTitle());
            videoCover.setImage(video.getImage());
            videoCovers.add(videoCover);
        }
        return videoCovers;
    }

    public VideoLink getVideoLink(int id){
        Video video=videoMapper.selectById(id);
        VideoLink videoLink=new VideoLink();
        videoLink.setTitle(video.getTitle());
        videoLink.setId(video.getId());
        videoLink.setLink(video.getLink());
        return videoLink;
    }

    public Video getVideo(int id){
        return videoMapper.selectById(id);
    }

    public void addVideo(String title, String cover,String videoKey){
        Video video=new Video();
        video.setTitle(title);
        video.setImage(qiniuUrl+cover);
        video.setLink(qiniuUrl+videoKey);
        videoMapper.insert(video);
    }

    public void deleteVideo(int id){
        videoMapper.deleteById(id);
    }
}
